package org.cangku.controller;

import org.cangku.dao.*;
import org.cangku.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/audit")
public class AuditController {
    @Autowired
    private LskucunDao lskucunDao;
    @Autowired
    private AuditDao auditDao;
    @Autowired
    private AuditInfoDao auditInfoDao;
    @Autowired
    private KuCunDao kuCunDao;
    @Autowired
    private ShopInfoDao shopInfoDao;
    /*从采购进货表中的数据   保存再审核表*/
    @RequestMapping("/cgshenhe")
    @ResponseBody
    public Map<String,Object> insert(String danhao,double yingshou,double shishou,int jbren,String beizhu){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Lskucun> lslist = lskucunDao.queryInfoByDingdan(danhao);
        Audit a = new Audit();
        a.setDanhao(danhao);
        a.setYingshou(yingshou);
        a.setShishou(shishou);
        a.setShuliang(lskucunDao.getNumByDanHao(danhao));
        a.setJbran(jbren);
        a.setBeizhu(beizhu);

        int i = auditDao.insertByCaiGou(a);
        if(i>0){
            for(Lskucun lsk : lslist){
                Auditinfo info = new Auditinfo();
                info.setAdingdan(lsk.getDingdan());
                info.setAsid(lsk.getSid());
                info.setAsname(lsk.getSname());
                info.setAjinjia(lsk.getJinjia());
                info.setAshoujia(lsk.getShoujia());
                info.setAjhnum(lsk.getJhnum());
                info.setApinpai(lsk.getPinpai());
                info.setAghsname(lsk.getGhsname());
                info.setAzongjine(lsk.getZongjine());
                info.setAsize(lsk.getSize());
                info.setAcolor(lsk.getColor());
                info.setAfenlei(lsk.getFenlei());

                auditInfoDao.insertByCG(info);
            }
            map.put("row",1);
        }else{
            map.put("row",0);
        }
        return map;
    }

    /*条件模糊查询*/
    @RequestMapping("/queryByDanhao")
    @ResponseBody
    public Map<String,Object> queryByDanhao(String danhao,int offset,int limit){
        Map<String,Object> map = new HashMap<String,Object>();
        //System.out.println("offset:"+offset);

        map.put("rows",auditDao.queryByDanhao(danhao,offset,limit));
        map.put("total",auditDao.getCount(danhao));

        return map;
    }

    /*修改审核状态*/
    @RequestMapping("/updateAuditStatic")
    @ResponseBody
    public Map<String,Object> updateAuditStatic(String danhao){
        Map<String,Object> map = new HashMap<String, Object>();
        int i = auditDao.updateAuditStatic(danhao);
        if(i>0){
            map.put("row",i);
            map.put("rows",auditDao.queryByDanhao("",0,5));
            map.put("total",auditDao.getCount(""));
            map.put("xiangqing",auditInfoDao.getInfoByDanHao("123"));
        }else{
            map.put("row",0);
        }
        return map;
    }

    /*审核通过*/
    @RequestMapping("/gettgshen")
    @ResponseBody
    public Map<String,Object> gettgshen(String danhao,int offset,int limit){
        Map<String,Object> map = new HashMap<String,Object>();
        //System.out.println("offset:"+offset);

        map.put("rows",auditDao.getDingDan(danhao,offset,limit));
        map.put("total",auditDao.getCounttg(danhao));

        return map;
    }

    /*入库*/
    @RequestMapping("/addkucun")
    @ResponseBody
    public Map<String,Object> addKucun(String danhao){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Auditinfo> list = auditInfoDao.getInfoByDanHao(danhao);
        for (Auditinfo ai : list){
            Kucun kc = new Kucun();
            kc.setSid(ai.getAsid());
            kc.setSname(ai.getAsname());
            kc.setSsize(ai.getAsize());
            kc.setScolor(ai.getAcolor());
            kc.setStype(ai.getAfenlei());
            kc.setSbrand(ai.getApinpai());
            kc.setGoldMoney(ai.getAjinjia());
            kc.setSaleMoney(ai.getAshoujia());
            kc.setStockNum(ai.getAjhnum());
            kc.setCostMoney(ai.getAzongjine());
            kc.setGhid(ai.getAghsname());
            /*判断库存是否存在*/
            int i = kuCunDao.kkInfoisextend(kc);
            if(i>0){
                /*修改库存数量和成本金额*/
                int upcg = kuCunDao.updateKuCunNum(kc);
                if(upcg>0){
                    String bianhao = String.valueOf(kc.getSid());
                    shopInfoDao.updateStockNum(bianhao,(int)kc.getStockNum());
                }
                auditDao.updateYiRuKu(danhao);
                map.put("row",1);
            }else{
                /*库存添加新数据*/
                int add = kuCunDao.insertKucunInfo(kc);
                if(add>0){
                    String bianhao = String.valueOf(kc.getSid());
                    /*判断商品信息表是否存在*/
                    int infocunzai = shopInfoDao.isextent(bianhao);
                    if(infocunzai>0){
                        /*如果存在增加数量*/
                        shopInfoDao.updateStockNum(bianhao,(int)kc.getStockNum());
                    }else{
                        /*不存在添加信息*/
                        Shopinfo info = new Shopinfo();
                        info.setSnumber(ai.getAsid());
                        info.setsName(ai.getAsname());
                        info.setStypeid(ai.getAfenlei());
                        info.setSbrand(ai.getApinpai());
                        info.setJinjia(ai.getAjinjia());
                        info.setShoujia(kc.getSaleMoney());
                        info.setStockNum(ai.getAjhnum());
                        shopInfoDao.addShopInfo(info);
                    }
                }
                auditDao.updateYiRuKu(danhao);
                map.put("row",1);
            }
        }
        List<Audit> aaa = new ArrayList<>();
        //map.put("dddd",aaa);
        return map;
    }


    /*采购单查询*/
    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> queryDan(String danhao,int adstid,String date1,String date2,int offset,int limit){
        Map<String,Object> map = new HashMap<String,Object>();
        /* List<Audit> list = auditDao.query(offset,limit,date,ctype,danhao);*/
        map.put("rows",auditDao.query(danhao,adstid,date1,date2,offset,limit));
        map.put("total",auditDao.getCountBytj(danhao,adstid,date1,date2));
        return map;
    }
}
